package com.nanhai.buddhist.college.module.collegebase.api.studentRoll;

import com.nanhai.buddhist.college.framework.common.enums.UserTypeEnum;
import com.nanhai.buddhist.college.framework.common.pojo.CommonResult;
import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.module.collegebase.api.student.StudentApi;
import com.nanhai.buddhist.college.module.collegebase.api.student.dto.StudentRespDTO;
import com.nanhai.buddhist.college.module.collegebase.api.student.dto.StudentUserRespDTO;
import com.nanhai.buddhist.college.module.collegebase.api.student.vo.UpdatePasswordVO;
import com.nanhai.buddhist.college.module.collegebase.api.teacher.vo.ClassesStudentListPageVO;
import com.nanhai.buddhist.college.module.collegebase.convert.studentroll.StudentRollConvert;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.studentroll.StudentRollDO;
import com.nanhai.buddhist.college.module.collegebase.service.studentroll.StudentRollService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

import static com.nanhai.buddhist.college.module.collegebase.enums.ApiConstants.VERSION;

/**
 * @author AKai
 * @date 2022/8/22
 */
@RestController // 提供 RESTful API 接口，给 Feign 调用
@DubboService(version = VERSION) // 提供 Dubbo RPC 接口，给 Dubbo Consumer 调用
@Validated
public class StudentRollApiImpl implements StudentApi {
    @Resource
    private StudentRollService studentRollService;


    @Override
    public CommonResult<StudentRespDTO> getStudentRespDTOById(String studentId) {
        StudentRollDO studentRoll = studentRollService.getStudentRoll(studentId);
        return CommonResult.success(StudentRollConvert.INSTANCE.convertStudentRespDTO(studentRoll));
    }

    @Override
    public CommonResult<List<StudentRespDTO>> getStudentRespDTOByIds(Collection<String> ids) {
        List<StudentRollDO> studentRollList = studentRollService.getStudentRollList(ids);
        return CommonResult.success(StudentRollConvert.INSTANCE.convertStudentRespDTOList(studentRollList));
    }

    @Override
    public CommonResult<PageResult<StudentRespDTO>> getStudentRespDTO(ClassesStudentListPageVO req) {
        PageResult<StudentRollDO> studentRolls = studentRollService.getStudentRespDTOByClassesId(req);
        return CommonResult.success(StudentRollConvert.INSTANCE.convertStudentRespDTOPage(studentRolls));
    }

    @Override
    public CommonResult<List<StudentRespDTO>> getStudentListByClassesId(String classesId) {
        List<StudentRollDO> studentRolls = studentRollService.getStudentListByClassesId(classesId);
        return CommonResult.success(StudentRollConvert.INSTANCE.convertStudentRespDTOList(studentRolls));
    }

    @Override
    public CommonResult<StudentUserRespDTO> getStudentByUserName(String studentUsername) {

        StudentRollDO studentRollDO = studentRollService.getStudentByUserName(studentUsername);

        if (Objects.nonNull(studentRollDO)) {
            StudentUserRespDTO build2 = StudentUserRespDTO.builder()
                    .studentId(studentRollDO.getId())
                    .password(studentRollDO.getPassword())
                    .userType(UserTypeEnum.STUDENT.getValue())
                    .build();
            return CommonResult.success(build2);
        } else {
            return CommonResult.success(null);
        }
    }

    @Override
    public CommonResult<Boolean> updatedStudentPassword(UpdatePasswordVO req) {
        return null;
    }
}
